package com.jake.xpage.page;

import android.content.Context;
import android.content.Intent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;


public class XPage extends FrameLayout implements LifecycleOwner {
    @NonNull
    protected final XPageConfig pageInfo;
    protected Callback mCallback;
    @NonNull
    protected final LifecycleRegistry mLifecycle;
    protected XPageArgument mArgument;

    public XPage(@NonNull Context context) {
        super(context);
        mLifecycle = new LifecycleRegistry(this);
        /**
         * 设置默认背景为白色，如果需要设置不同的背景颜色，可以在子类中修改
         */
        setBackgroundColor(0xffffffff);
        pageInfo = new XPageConfig();
        configPageInfo(pageInfo);
        setFitsSystemWindows(true);
    }

    public XPageArgument getArgument() {
        return mArgument;
    }

    public void setArgument(XPageArgument argument) {
        this.mArgument = argument;
    }

    protected void configPageInfo(@NonNull XPageConfig pageInfo) {

    }

    @NonNull
    public XPageConfig getPageInfo() {
        return pageInfo;
    }

    public void setCallback(Callback callback) {
        this.mCallback = callback;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    }

    public void startPage(XPage page, IPageAnimator animator) {
        if (mCallback != null) {
            mCallback.startPage(page, animator);
        }
    }

    public void startPage(XPage page, boolean animate) {
        if (mCallback != null) {
            mCallback.startPage(page, animate);
        }
    }

    public void startPage(XPage page) {
        startPage(page, true);
    }

    public boolean onBackPressed() {
        finish();
        return true;
    }

    protected void finish() {
        finish(true);
    }

    protected void finish(boolean animated) {
        if (mCallback != null) {
            mCallback.finish(this, animated);
        }
    }

    protected void finish(IPageAnimator animator) {
        if (mCallback != null) {
            mCallback.finish(this, animator);
        }
    }

    protected void onCreate() {

    }

    protected void onStart() {

    }

    protected void onResume() {

    }

    protected void onPause() {

    }

    protected void onStop() {

    }

    protected void onDestroy() {

    }

    protected LifecycleEventObserver mLifecycleEventObserver;

    public LifecycleEventObserver getLifecycleEventObserver() {
        if (mLifecycleEventObserver == null) {
            mLifecycleEventObserver = new LifecycleEventObserver() {
                @Override
                public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event) {

                    switch (event) {
                        case ON_CREATE:
                            onCreate();
                            break;
                        case ON_START:
                            onStart();
                            break;
                        case ON_RESUME:
                            onResume();
                            break;
                        case ON_PAUSE:
                            onPause();
                            break;
                        case ON_STOP:
                            onStop();
                            break;
                        case ON_DESTROY:
                            mCallback = null;
                            onDestroy();
                            break;
                    }
                    mLifecycle.handleLifecycleEvent(event);
                }
            };
        }
        return mLifecycleEventObserver;
    }

    @NonNull
    @Override
    public LifecycleRegistry getLifecycle() {
        return mLifecycle;
    }

    public interface Callback {
        void finish(@NonNull XPage page, @Nullable IPageAnimator animator);

        void finish(@NonNull XPage page, boolean animate);

        void startPage(@NonNull XPage page, @Nullable IPageAnimator animator);

        void startPage(@NonNull XPage page, boolean useDefaultAnimate);
    }
}
